<script setup lang="ts">
import CodeBlock from '@components/CodeBlock.vue'

// 使用defineSlots定义插槽类型
defineSlots<{
  // 默认插槽，接收user对象参数
  default?: (props: { user: { name: string; age: number } }) => any
  // 头部插槽，无参数
  header?: () => any
  // 底部插槽，接收message参数
  footer?: (props: { message: string }) => any
}>()

// 模拟用户数据
const userData = {
  name: '张三',
  age: 30
}

const footerMessage = '感谢使用Vue 3编译器宏'

// 插槽信息描述
const slotsInfo = {
  title: 'defineSlots 功能演示',
  description: '类型安全的插槽定义和使用',
  slots: [
    { name: '默认插槽', props: ['user'], description: '接收用户信息对象' },
    { name: 'header 插槽', props: [], description: '可选的头部内容' },
    { name: 'footer 插槽', props: ['message'], description: '接收页脚信息' }
  ]
}
</script>

<template>
  <div class="compiler-slots-demo">
    <h3>defineSlots 演示</h3>
    
    <div class="demo-content">
      <div class="slots-container">
        <!-- 头部插槽 -->
        <header class="slot-header">
          <slot name="header" />
          <p v-if="!$slots.header" class="default-slot">默认头部内容</p>
        </header>
        
        <!-- 默认插槽，传递user数据 -->
        <main class="slot-main">
          <slot :user="userData" />
          <p v-if="!$slots.default" class="default-slot">
            默认内容 - 用户名: {{ userData.name }}, 年龄: {{ userData.age }}
          </p>
        </main>
        
        <!-- 底部插槽，传递message -->
        <footer class="slot-footer">
          <slot name="footer" :message="footerMessage" />
          <p v-if="!$slots.footer" class="default-slot">{{ footerMessage }}</p>
        </footer>
      </div>
      
      <div class="code-section">
        <h4>{{ slotsInfo.title }}</h4>
        <p>{{ slotsInfo.description }}</p>
        <ul>
          <li v-for="slot in slotsInfo.slots" :key="slot.name">
            • {{ slot.name }} - {{ slot.description }}
            <span v-if="slot.props.length > 0"> (参数: {{ slot.props.join(', ') }})</span>
          </li>
        </ul>
      </div>
    </div>
    
    <div class="explanation">
      <h4>defineSlots 的优势</h4>
      <ul>
        <li>✅ 为插槽提供完整的 TypeScript 类型支持</li>
        <li>✅ IDE 可以提供准确的类型提示和自动补全</li>
        <li>✅ 编译时类型检查，避免运行时错误</li>
        <li>✅ 更清晰的组件接口文档</li>
        <li>✅ 支持定义插槽参数的类型</li>
      </ul>
    </div>
    
    <div class="usage-tips">
      <h4>使用提示</h4>
      <p>当父组件使用此组件时，在模板中访问插槽参数会获得完整的类型支持：</p>
      <CodeBlock language="html">
        <CompilerSlotsDemo>
          <template #default="{ user }">
            <!-- 这里 user 会有完整的类型提示 -->
            <p>姓名: {{ user.name }}</p>
            <p>年龄: {{ user.age }}</p>
          </template>
        </CompilerSlotsDemo>
      </CodeBlock>
    </div>
  </div>
</template>

<style scoped>
.compiler-slots-demo {
  background-color: white;
  border-radius: 8px;
  padding: 1.5rem;
}

.compiler-slots-demo h3 {
  color: #42b883;
  margin-bottom: 1rem;
}

.demo-content {
  margin-bottom: 1.5rem;
}

.slots-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background-color: #f8f9fa;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 1.5rem;
  border: 1px solid #e9ecef;
}

.slot-header {
  background-color: #e3f2fd;
  padding: 1rem;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.slot-main {
  background-color: white;
  padding: 2rem;
  min-height: 100px;
  flex: 1;
}

.slot-footer {
  background-color: #fff3e0;
  padding: 1rem;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.default-slot {
  color: #666;
  font-style: italic;
  margin: 0;
}

.code-section {
  background-color: #f8f9fa;
  padding: 1rem;
  border-radius: 6px;
  margin-bottom: 1.5rem;
}

.code-section h4 {
  margin-top: 0;
  margin-bottom: 1rem;
  color: #333;
}

.explanation {
  background-color: #e8f5e9;
  padding: 1.5rem;
  border-radius: 6px;
  border-left: 4px solid #4caf50;
  margin-bottom: 1.5rem;
}

.explanation h4 {
  margin-top: 0;
  margin-bottom: 1rem;
  color: #2e7d32;
}

.explanation ul {
  margin: 0;
  padding-left: 1.5rem;
}

.explanation li {
  margin-bottom: 0.5rem;
  color: #333;
}

.usage-tips {
  background-color: #e3f2fd;
  padding: 1.5rem;
  border-radius: 6px;
  border-left: 4px solid #1976d2;
}

.usage-tips h4 {
  margin-top: 0;
  margin-bottom: 1rem;
  color: #0d47a1;
}

.usage-tips pre {
  background-color: #f5f5f5;
  padding: 1rem;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0;
}

.usage-tips code {
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: 0.875rem;
  color: #333;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .slots-container {
    gap: 0.5rem;
  }
  
  .slot-main {
    padding: 1rem;
  }
}
</style>